<?php

/**
 * @author Xubi Nguyen
 * @copyright 2011
 */

class login_model extends CI_Model
{
	function __construct(){
		parent::__construct('dangnhap');
	}
	function Get($session){
		$login = new stdClass();
		$login = $this->where('sessionid',"'".$session."'",false)->find();
        if(empty($login->sessionid))
            return false;
        else{ 
           $data = json_decode($login->account ,true);   
           $data['time'] = $login->time;
           return $data;       
        }
		return false;
	}
	function permission($ctronller){
		$login = $this->pub->GetSession('login');
		$groupid = $login['groups'];
		$right = $this->model->table('groups')
							 ->select('params')
							 ->where('id',$groupid)
							 ->find();
		$menu = $this->model->table('menus')
						    ->select('id')
							->where('isdelete',0)
							->where('route',$ctronller)
							->find();
		if(!empty($menu->id)){
			$idmenu = $menu->id;
		}
		else{
			$idmenu = 0;
		}
		if(!empty($right->params)){
			$obj = json_decode($right->params,true);
			if(isset($obj[$idmenu])){
				return $obj[$idmenu];
			}
			else{
				return "";
			}
		}
		else{
			return "";
		}
	}
    function SetTime($session){
		$this->where('sessionid',"'".$session."'",false)->update(array('time'=>gmdate("Y-m-d H:i:s",time()+7*3600)));
	}	
    function Del($session){
		$this->where('sessionid',"'".$session."'",false)->delete();
	}
    function DeleteAll($time){
	   $datenow = gmdate("Y-m-d H:i:s",time()+7*3600);
       $timenow = strtotime($datenow);
       $timesub = $timenow - $time;
	   $this->where('time <= ',date("Y-m-d H:i:s",$timesub))->delete();
	}
	
	public function ketnoi_sql($sql='',$kieuthucthi=''){
		//thuc hien webservice
		$link = "https://115.78.128.158/soap/server.php";
		//$this->load->helper('nusoap');
		$this->load->helper('nusoap');
		//$sql="SELECT `id`, `ten` FROM (`trungtam`) WHERE `thungrac` = 0";
		$param = array('sql' => $sql, 'db' => 'quanlyuser');
		$client = new nusoap_client($link);
		//////////////////////////////////
		$client->setCredentials("viettel", "viettel@1234", "basic");

		/////////////////////////////////
		$client->soap_defencoding = 'UTF-8';
		$client->decode_utf8 = false;
		if($kieuthucthi=='get'){
			$response = $client->call('getdata', $param);
			if($client->fault){
				$linkphu = "https://10.30.72.11/soap/server.php";
				$clients = new nusoap_client($linkphu);
				//////////////////////////////////
				$clients->setCredentials("viettel", "viettel@1234", "basic");
		
				/////////////////////////////////
				$clients->soap_defencoding = 'UTF-8';
				$clients->decode_utf8 = false;
				$responsephu = $clients->call('getdata', $param);
				if($clients->fault){
					return "FAULT: <p>Code: (".$clients->faultcode.")</p>String: ".$clients->faultstring;
				}
				else
					return $responsephu;
			}
			else{
				return $response;
			}
		}
		else{	//print_r($sql);exit;
			$response = $client->call('sqlexecute', $param);
		//=============S upadte du lieu vao them database du phong=====================
			$linkphu ="https://10.30.72.11/soap/server.php";
			$clients = new nusoap_client($linkphu);
			$clients->soap_defencoding = 'UTF-8';
			$clients->decode_utf8 = false;
			$responsephu = $clients->call('sqlexecute', $param);
       //=============E upadte du lieu vao them database du phong=====================
			if($client->fault){
				return "FAULT: <p>Code: (".$client->faultcode.")</p>String: ".$client->faultstring;
			}
			else
			{
				return $response;
			}
		}
	}
}
?>